package com.web.exiaodao.business.controller;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.web.webstart.base.constant.XaConstant;
import com.web.webstart.base.controller.BaseController;
import com.web.webstart.base.exception.BusinessException;
import com.web.webstart.base.util.jpa.WebUitl;
import com.web.webstart.base.util.result.XaResult;
import com.web.exiaodao.business.entity.FanliRules;
import com.web.exiaodao.business.service.FanliRulesService;
import com.web.exiaodao.remote.vo.FanliRulesVo;

/**
 * @Title: FanliRulesController.java
 * @Package com.web.webstart.business.controller
 * @Description: 返利-合约表控制器
 * @author eason.zt
 * @date 2017-02-13 14:31:04
 * @version V1.0
 */
@Controller
@RequestMapping("/cms/fanliRules")
public class FanliRulesController extends BaseController {

	private static final Logger LOGGER = Logger.getLogger(FanliRulesController.class);

	@Autowired
	private FanliRulesService fanliRulesService;

	/**
	 * @Title: findFanliRulesList
	 * @Description: 分页查询FanliRules	 * @param nextPage
	 * @param pageSize
	 * @param status		//默认查询非删除3的数据,具体类型参照XaConstant.Status
	 * //@param sortData
	 * @param jsonFilter
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="findFanliRulesList",method=RequestMethod.POST)
	public XaResult<Page<FanliRulesVo>> findFanliRulesList(
		@RequestParam(defaultValue = "0") Integer nextPage,
		@RequestParam(defaultValue = "10") Integer pageSize,
		@RequestParam(defaultValue = "3") Integer status,
		@RequestParam(defaultValue = "[{property:'createTime',direction:'DESC'}]") String sortData,
		@RequestParam(defaultValue = "{}") String jsonFilter
	) throws BusinessException{
		XaResult<Page<FanliRulesVo>> xr = new XaResult<Page<FanliRulesVo>>();
		try{
			Pageable pageable = WebUitl.buildPageRequest(nextPage, pageSize, sortData);
			Map<String,Object> filterParams =  WebUitl.getParametersStartingWith(jsonFilter, "search_");
			return fanliRulesService.findFanliRulesList(status, filterParams, pageable);
		}catch(Exception e){
			LOGGER.error(XaConstant.Message.error,new Throwable(e));
			xr.error(XaConstant.Message.error);
		}
		return xr;
	}

	/**
	 * @Title: findFanliRulesEQStatusPage
	 * @Description: (预留)分页查询FanliRules	 * @param nextPage
	 * @param pageSize
	 * @param status		//默认查询正常状态1的数据,具体类型参照XaConstant.Status
	 * //@param sortData
	 * @param jsonFilter
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="findFanliRulesEQStatusPage",method=RequestMethod.POST)
	public XaResult<Page<FanliRules>> findFanliRulesEQStatusPage(
		@RequestParam(defaultValue = "0") Integer nextPage,
		@RequestParam(defaultValue = "10") Integer pageSize,
		@RequestParam(defaultValue = "1") Integer status,
		@RequestParam(defaultValue = "[{property:'createTime',direction:'DESC'}]") String sortData,
		@RequestParam(defaultValue = "{}") String jsonFilter
	) throws BusinessException{
		XaResult<Page<FanliRules>> xr = new XaResult<Page<FanliRules>>();
		try{
			Pageable pageable = WebUitl.buildPageRequest(nextPage, pageSize, sortData);
			Map<String,Object> filterParams =  WebUitl.getParametersStartingWith(jsonFilter, "search_");
			xr = fanliRulesService.findListEQStatusByFilter(status, filterParams, pageable);
		}catch(Exception e){
			xr.error(e instanceof  BusinessException ? e.getMessage() : XaConstant.Message.error);
			LOGGER.error(XaConstant.Message.error + e.getMessage() , new Throwable(e));
        }
		return xr;
	}

	/**
	 * @Title: findFanliRulesById
	 * @Description: 根据ID查找单条实体
	 * @param modelId
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="findFanliRulesById",method=RequestMethod.POST)
	public XaResult<FanliRulesVo> findFanliRulesById(
		@RequestParam Long modelId
	) throws BusinessException{
		XaResult<FanliRulesVo> xr = new XaResult<FanliRulesVo>();
		try{
			xr = fanliRulesService.findFanliRulesById(modelId);
		}catch(Exception e){
			xr.error(e instanceof  BusinessException ? e.getMessage() : XaConstant.Message.error);
			LOGGER.error(XaConstant.Message.error + e.getMessage() , new Throwable(e));
        }
		return xr;
	}
	/**
	 * @Title: saveModel
	 * @Description: 保存实体数据
	 * @param model
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="saveFanliRules",method=RequestMethod.POST)
	public XaResult<FanliRules> saveFanliRules(
        HttpServletRequest request,
		FanliRules model
	) throws BusinessException{
		XaResult<FanliRules> xr = new XaResult<FanliRules>();
		try{
			model.setCreateUser(request.getSession().getAttribute(XaConstant.SessionKey.currentUser).toString());
			xr = fanliRulesService.saveOrUpdate(model);
		}catch(Exception e){
			xr.error(e instanceof  BusinessException ? e.getMessage() : XaConstant.Message.error);
			LOGGER.error(XaConstant.Message.error + e.getMessage() , new Throwable(e));
        }
		return xr;
	}

	/**
	 * @Title: multiOperateFanliRulesByIds
	 * @Description: 批量操作多个实体状态,根据status进行操作
	 * @param modelIds    	多个实体id,中间用逗号隔开
	 * @param status 		操作类型,status类型见XaConstant.Status,默认删除操作
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="operateFanliRulesByIds",method=RequestMethod.POST)
	public XaResult<FanliRules> operateFanliRulesByIds(
		HttpServletRequest request,
		@RequestParam String modelIds,
		@RequestParam(defaultValue = "3") Integer status
	) throws BusinessException{
		XaResult<FanliRules> xr = new XaResult<FanliRules>();
		try{
			xr =  fanliRulesService.multiOperate(modelIds,status);
		}catch(Exception e){
			xr.error(e instanceof  BusinessException ? e.getMessage() : XaConstant.Message.error);
			LOGGER.error(XaConstant.Message.error + e.getMessage() , new Throwable(e));
        }
		return xr;
	}

}

